package GUI;

import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import logic.Server;

@SuppressWarnings("serial")
public class ReclaimPanel extends JPanel {

	
	public Server server;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField sizeReclaim;
	private JTextField percReclaim;
	public ReclaimPanel(final Server server) {
		this.server = server;
		
		final int occupiedSpace = (int) ((int) (100 * Server.getChunkRepository().getRepositorySpaceOccupied())/Server.getChunkRepository().getRepositorySize());
		
		JLabel lblReclaimSpace = new JLabel("Reclaim Space");
		lblReclaimSpace.setFont(new Font("Tahoma", Font.PLAIN, 18));
		
		JLabel lblUsedSpace = new JLabel("Used Space");
		JLabel lblSizeoccupied;
		NumberFormat nf = new DecimalFormat ("#,##0.00");  
		double sizeOccupiedKB= Server.getChunkRepository().getRepositorySpaceOccupied()/1024;
		if(sizeOccupiedKB<1024 )
			lblSizeoccupied = new JLabel(String.valueOf(nf.format(sizeOccupiedKB)) + " KB");
		else{
			
			double sizeOccupiedMB= (double)Server.getChunkRepository().getRepositorySpaceOccupied()/(1024*1024);
			lblSizeoccupied = new JLabel(String.valueOf(nf.format(sizeOccupiedMB)) + " MB");
		}
		
		JProgressBar progressBar = new JProgressBar();
		progressBar.setValue(occupiedSpace);
		
		final JRadioButton rdbtnReclaimSpacePercentage = new JRadioButton("Reclaim Space Percentage");
		rdbtnReclaimSpacePercentage.setSelected(true);
		rdbtnReclaimSpacePercentage.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				if(arg0.getStateChange()== ItemEvent.SELECTED)
				{
					percReclaim.setEditable(true);
				}
				else if(arg0.getStateChange()== ItemEvent.DESELECTED)
				{
					percReclaim.setEditable(false);
				}
			}
		});
		
		JLabel lblPercoccupied = new JLabel(String.valueOf(occupiedSpace)+"%");
		
		
		buttonGroup.add(rdbtnReclaimSpacePercentage);
		
		percReclaim = new JTextField();
		percReclaim.setColumns(10);
		
		JLabel lblinMb_1 = new JLabel("(in %)");
		
		final JRadioButton rdbtnReclaimSpaceSize = new JRadioButton("Reclaim Space Size");
		rdbtnReclaimSpaceSize.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				if(arg0.getStateChange()== ItemEvent.SELECTED)
				{
					sizeReclaim.setEditable(true);
				}
				else if(arg0.getStateChange()== ItemEvent.DESELECTED)
				{
					sizeReclaim.setEditable(false);
				}
			}
		});
		buttonGroup.add(rdbtnReclaimSpaceSize);
		
		sizeReclaim = new JTextField();
		sizeReclaim.setEditable(false);
		sizeReclaim.setColumns(10);
		
		JLabel lblinMb = new JLabel("(in MB)");
		
		JButton btnReclaimSpace = new JButton("Reclaim Space");
		btnReclaimSpace.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				if(rdbtnReclaimSpacePercentage.isSelected())
				{
					try{
						int perc =(int) Double.parseDouble(percReclaim.getText());
						int size = (int) ((perc * Server.getChunkRepository().getRepositorySize())/100);
						if(perc > occupiedSpace || perc <0 )
						{
							JFrame frame = new JFrame();
							JOptionPane.showMessageDialog(frame,
								    "Percentage Invalid.",
								    "Inane error",
								    JOptionPane.ERROR_MESSAGE);
						}
						else{ 
							server.reclaimSpace(size, false);
							GUI.frame.setContentPane(new MainPanel(server));
							((JPanel)GUI.frame.getContentPane()).revalidate();
							GUI.frame.repaint();
						}
					}
					catch(NumberFormatException e1)
					{
						JFrame frame = new JFrame();
						JOptionPane.showMessageDialog(frame,
							    "Percentage Format Invalid.",
							    "Inane error",
							    JOptionPane.ERROR_MESSAGE);
					}
				}
				else if(rdbtnReclaimSpaceSize.isSelected())
				{
					try{
						long size = Long.parseLong(sizeReclaim.getText())*1024*1024;
						if(size> Server.getChunkRepository().getRepositorySpaceOccupied() || size<0){
							JFrame frame = new JFrame();
							JOptionPane.showMessageDialog(frame,
								    "Size Invalid.",
								    "Inane error",
								    JOptionPane.ERROR_MESSAGE);
						}
						else
						{
							server.reclaimSpace(size, false);
							GUI.frame.setContentPane(new MainPanel(server));
							((JPanel)GUI.frame.getContentPane()).revalidate();
							GUI.frame.repaint();
						}
						
					}
					catch(NumberFormatException e1)
					{
						JFrame frame = new JFrame();
						JOptionPane.showMessageDialog(frame,
							    "Size Format Invalid.",
							    "Inane error",
							    JOptionPane.ERROR_MESSAGE);
					}
					
				}
			}
		});
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				GUI.frame.setContentPane(new MainPanel(server));
				((JPanel)GUI.frame.getContentPane()).revalidate();
				GUI.frame.repaint();
			}
		});
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(197)
					.addComponent(lblReclaimSpace))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(93)
					.addComponent(rdbtnReclaimSpacePercentage)
					.addGap(35)
					.addComponent(percReclaim, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
					.addGap(6)
					.addComponent(lblinMb_1))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(93)
					.addComponent(rdbtnReclaimSpaceSize)
					.addGap(71)
					.addComponent(sizeReclaim, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
					.addGap(5)
					.addComponent(lblinMb))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(118)
					.addComponent(btnReclaimSpace)
					.addGap(94)
					.addComponent(btnCancel))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(93)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(progressBar, GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
							.addGap(18)
							.addComponent(lblPercoccupied)
							.addGap(122))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblUsedSpace)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lblSizeoccupied)
							.addGap(258))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(30)
					.addComponent(lblReclaimSpace)
					.addGap(34)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblUsedSpace)
						.addComponent(lblSizeoccupied))
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(5)
							.addComponent(progressBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblPercoccupied)))
					.addGap(34)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(rdbtnReclaimSpacePercentage)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(1)
							.addComponent(percReclaim, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(4)
							.addComponent(lblinMb_1)))
					.addGap(5)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(rdbtnReclaimSpaceSize)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(1)
							.addComponent(sizeReclaim, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(4)
							.addComponent(lblinMb)))
					.addGap(35)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(btnReclaimSpace)
						.addComponent(btnCancel)))
		);
		setLayout(groupLayout);

	}

}
